<?php
namespace backend\controllers;
use backend\models\Article;
use backend\models\searchs\Article as ArticleSearch;
use backend\models\Category;
use backend\models\Links;
use backend\models\Tags;
use yii\web\Controller;
use Yii;

class ArticleController extends Controller{

    public function actions(){
        return [
            'uploads'=>[
                'class' => 'common\widgets\ueditor\UeditorAction',
                'config'=>[
                    //上传图片配置
                    'imageUrlPrefix' => "", /* 图片访问路径前缀 */
                    'imagePathFormat' => "/upload/{yyyy}{mm}{dd}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */

                ]
            ],
            'upload'=>[
                'class' => 'common\widgets\file_upload\UploadAction',
                'config' => [
                    'imagePathFormat' => "/upload/{yyyy}{mm}{dd}{rand:6}",
                ]
            ],
        ];
    }

    public function actionAdd(){
        $model = new Article();
        $model->scenario = 'add_art';
        $cate_model = new Category();
        $list = $cate_model->getOptions();
        $list[0] = '请选择';
        $tags = Tags::find()->asArray()->all();
        foreach($tags as $k => $v){
            $newtag[$v['tagid']] = $v['tagname'];
        }
        if(Yii::$app->request->isPost){
           $data = Yii::$app->request->post();
            if($model->load($data) && $model->validate()){
                $model->create_time = $model->update_time = time();
                $model->category_id = $data['Article']['category_id'];
                $model->image = $data['Article']['image'];
                if($model->save()){
                    if(isset($data['Article']['tags']) && !empty($data['Article']['tags'])){
                        foreach($data['Article']['tags'] as $k => $v){
                            $link_model = new Links();
                            $link_model->article_id = $model->articleid;
                            $link_model->tagid = $v;
                            $link_model->create_time = time();
                            $link_model->save();
                        }
                    }
                    Yii::$app->session->setFlash('info', '添加文章成功');
                }
            }
        }
        return $this->render('art_add', [
            'model' => $model,
            'list' => $list,
            'tags' => $newtag,
        ]);
    }

    public function actionList(){
        $searchModel = new ArticleSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('art_list', [
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,
        ]);
    }

    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['add']);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    protected function findModel($id)
   {
       if (($model = News::findOne($id)) !== null) {
           return $model;
       } else {
           throw new NotFoundHttpException('The requested page does not exist.');
       }
   }
}
